<template>
  <div class="page">
    <div class="countdown">
      <p class="countdown-text">{{ countdown }} 秒后自动跳转到首页</p>
      <el-button type="primary" @click="goHome">立即返回首页</el-button>
    </div>
  </div>
</template>

<script setup>
import { defineOptions, ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

defineOptions({
  name: 'NotFound'
})

const router = useRouter()
const countdown = ref(3)
let timer = null

// 跳转到首页
const goHome = () => {
  if (timer) {
    clearInterval(timer)
    timer = null
  }
  router.push('/')
}

// 开始倒计时
const startCountdown = () => {
  timer = setInterval(() => {
    countdown.value--
    if (countdown.value <= 0) {
      clearInterval(timer)
      timer = null
      goHome()
    }
  }, 1000)
}

onMounted(() => {
  ElMessage.warning('页面不存在，即将跳转到首页')
  startCountdown()
})

onUnmounted(() => {
  if (timer) {
    clearInterval(timer)
    timer = null
  }
})
</script>

<style scoped lang="scss">
.page {
  width: 100vw;
  height: 100vh;
  background: url('@/assets/img/404.png') no-repeat center center;
  background-size: cover;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.countdown {
  position: absolute;
  bottom: 100px;
  left: 50%;
  transform: translateX(-50%);
  text-align: center;
  background: rgba(255, 255, 255, 0.9);
  padding: 20px 40px;
  border-radius: 10px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.countdown-text {
  font-size: 18px;
  color: #333;
  margin-bottom: 15px;
  font-weight: 500;
}
</style>
